#include <string.h>
#include <unistd.h>
#include <math.h>
#include "los_task.h"
#include "ohos_init.h"
#include "lz_hardware.h"
#include "keyScan.h"

uint8_t getpasslenth(int pass)
{
    uint8_t lenth;
    lenth = 0;
    if (pass > 0)
    {
        while (pass != 0)
        {
            pass = pass / 10;
            lenth++;
        }
    }
    return lenth;
}

void key_Init()
{
    // set hang GPIO INPUT
    LzGpioInit(R1);
    PinctrlSet(R1, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(R1, LZGPIO_DIR_OUT);

    LzGpioInit(R2);
    PinctrlSet(R2, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(R2, LZGPIO_DIR_OUT);

    LzGpioInit(R3);
    PinctrlSet(R3, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(R4, LZGPIO_DIR_OUT);

    LzGpioInit(R4);
    PinctrlSet(R4, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(R4, LZGPIO_DIR_OUT);

    LzGpioInit(C1);
    PinctrlSet(C1, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(C1, LZGPIO_DIR_IN);
    LzGpioSetVal(C1,0);

    LzGpioInit(C2);
    PinctrlSet(C2, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(C2, LZGPIO_DIR_IN);
    LzGpioSetVal(C2,0);

    LzGpioInit(C3);
    PinctrlSet(C3, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(C3, LZGPIO_DIR_IN);
    LzGpioSetVal(C3,0);

    LzGpioInit(C4);
    PinctrlSet(C4, MUX_FUNC0, PULL_KEEP, DRIVE_LEVEL0);
    LzGpioSetDir(C4, LZGPIO_DIR_IN);
    LzGpioSetVal(C4,0);
    printf("keyInit:\n");
}

int getKeyVal()
{
    int key_Value;
    key_Value = 0;
    printf("111111111111111111111111\n");
    LzGpioValue value11;
    LzGpioValue value2;
    LzGpioValue value3;
    LzGpioValue value4;

    LzGpioSetVal(R1, 0);
    LzGpioSetVal(R2, 0);
    LzGpioSetVal(R3, 0);
    LzGpioSetVal(R4, 0);

    LzGpioGetVal(C1, &value11);
    LzGpioGetVal(C2, &value2);
    LzGpioGetVal(C3, &value3);
    LzGpioGetVal(C4, &value4);

    printf("%d\n", value11);
    printf("%d\n", value2);
    printf("%d\n", value3);
    printf("%d\n", value4);

    if (value11 != 1 || value2 != 1 || value3 != 1 || value4 != 1)
    {
        LzGpioSetVal(R1, 1);
        LzGpioSetVal(R2, 0);
        LzGpioSetVal(R3, 0);
        LzGpioSetVal(R4, 0);

        LzGpioGetVal(C1, &value11);
        LzGpioGetVal(C2, &value2);
        LzGpioGetVal(C3, &value3);
        LzGpioGetVal(C4, &value4);

        if (value11 == 1 && value2 == 0 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 1);
            key_Value = 1;
        }
        if (value11 == 0 && value2 == 1 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 1);
            key_Value = 2;
        }
        if (value11 == 0 && value2 == 0 && value3 == 1 && value4 == 0)
        {
            printf("value:%d\n", 1);
            key_Value = 3;
        }
        if (value11 == 0 && value2 == 0 && value3 == 0 && value4 == 1)
        {
            printf("value:%d\n", 1);
            key_Value = 4;
        }

        do
        {
            LzGpioGetVal(C1, &value11);
            LzGpioGetVal(C2, &value2);
            LzGpioGetVal(C3, &value3);
            LzGpioGetVal(C4, &value4);
        } while (value11 > 0 | value2 > 0 | value3 > 0 | value4 > 0);

        LzGpioSetVal(R1, 0);
        LzGpioSetVal(R2, 1);
        LzGpioSetVal(R3, 0);
        LzGpioSetVal(R4, 0);

        LzGpioGetVal(C1, &value11);
        LzGpioGetVal(C2, &value2);
        LzGpioGetVal(C3, &value3);
        LzGpioGetVal(C4, &value4);

        if (value11 == 1 && value2 == 0 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 5);
            key_Value = 5;
        }
        if (value11 == 0 && value2 == 1 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 6);
            key_Value = 6;
        }
        if (value11 == 0 && value2 == 0 && value3 == 1 && value4 == 0)
        {
            printf("value:%d\n", 7);
            key_Value = 7;
        }
        if (value11 == 0 && value2 == 0 && value3 == 0 && value4 == 1)
        {
            printf("value:%d\n", 8);
            key_Value = 8;
        }

        do
        {
            LzGpioGetVal(C1, &value11);
            LzGpioGetVal(C2, &value2);
            LzGpioGetVal(C3, &value3);
            LzGpioGetVal(C4, &value4);
        } while (value11 > 0 | value2 > 0 | value3 > 0 | value4 > 0);

        LzGpioSetVal(R1, 0);
        LzGpioSetVal(R2, 0);
        LzGpioSetVal(R3, 1);
        LzGpioSetVal(R4, 0);

        LzGpioGetVal(C1, &value11);
        LzGpioGetVal(C2, &value2);
        LzGpioGetVal(C3, &value3);
        LzGpioGetVal(C4, &value4);

        if (value11 == 1 && value2 == 0 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 9);
            key_Value = 9;
        }
        if (value11 == 0 && value2 == 1 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 10);
            key_Value = 10;
        }
        if (value11 == 0 && value2 == 0 && value3 == 1 && value4 == 0)
        {
            printf("value:%d\n", 11);
            key_Value = 11;
        }
        if (value11 == 0 && value2 == 0 && value3 == 0 && value4 == 1)
        {
            printf("value:%d\n", 12);
            key_Value = 12;
        }

        do
        {
            LzGpioGetVal(C1, &value11);
            LzGpioGetVal(C2, &value2);
            LzGpioGetVal(C3, &value3);
            LzGpioGetVal(C4, &value4);
        } while (value11 > 0 | value2 > 0 | value3 > 0 | value4 > 0);

        LzGpioSetVal(R1, 0);
        LzGpioSetVal(R2, 0);
        LzGpioSetVal(R3, 0);
        LzGpioSetVal(R4, 4);

        LzGpioGetVal(C1, &value11);
        LzGpioGetVal(C2, &value2);
        LzGpioGetVal(C3, &value3);
        LzGpioGetVal(C4, &value4);

        if (value11 == 1 && value2 == 0 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 13);
            key_Value = 13;
        }
        if (value11 == 0 && value2 == 1 && value3 == 0 && value4 == 0)
        {
            printf("value:%d\n", 14);
            key_Value = 14;
        }
        if (value11 == 0 && value2 == 0 && value3 == 1 && value4 == 0)
        {
            printf("value:%d\n", 15);
            key_Value = 15;
        }
        if (value11 == 0 && value2 == 0 && value3 == 0 && value4 == 1)
        {
            printf("value:%d\n", 16);
            key_Value = 16;
        }

        do
        {
            LzGpioGetVal(C1, &value11);
            LzGpioGetVal(C2, &value2);
            LzGpioGetVal(C3, &value3);
            LzGpioGetVal(C4, &value4);
        } while (value11 > 0 | value2 > 0 | value3 > 0 | value4 > 0);
    }
    else
    {
        printf("no one\n");
        return key_Value;
    }

    return key_Value;
}